//
//  CityPicker.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/22.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit
import SwiftyJSON

class GFCityPicker: UIViewController {

    var handle:SLBlock?
    var datasA:[JSON] = []
    var datasB:[JSON] = []
    var datasC:[JSON] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        self.closeBtn.isHidden = false
        self.view.backgroundColor = rgba(hex: "#000000", a: 0.7)
        self.titleL.text = "Address"
        self.nextButton.setTitle("Confirm", for: .normal)
     
        if let list = __valueForKey(key: KCityList) as? [Any]{
            for data in list {
                self.datasA.append(JSON(data))
            }
            if self.datasA.count > 0{
                let arr = self.datasA[0]["quite"].arrayValue
                self.datasB = arr
            }
            if self.datasB.count > 0 {
                let arr = self.datasB[0]["quite"].arrayValue
                self.datasC = arr
            }

            self.picker.reloadAllComponents()
        }
    }
    class func showCityPick( page:UIViewController,handle: SLBlock?){
        let picker = GFCityPicker()
        picker.handle = handle
        picker.modalPresentationStyle = .overCurrentContext
        picker.modalTransitionStyle = .crossDissolve
        page.present(picker, animated: true)
    }
    lazy var backView:UIView = {
        let pt = create_view(bgColor: .white, supView: self.view) { make in
            make.height.equalTo(435)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
        pt.layer.masksToBounds = true
        pt.layer.cornerRadius = 12
        return pt
    }()

    lazy var titleL:UILabel = {
        let pt = create_label(text: "Correct demonstration", textColor: .black,textFont: SLFontConfig.medium18,alignment: .center, supView: self.backView) { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(24)
        }
        pt.numberOfLines = 0
        return pt
    }()
   

    lazy var closeBtn:UIButton = {
        let pt = create_button(image: IMAGE("icon_return"), supView: self.backView, closure: { make in
            make.top.equalTo(25)
            make.left.equalTo(15)
            make.height.width.equalTo(24)
        }, selector: (target: self, action: #selector(buttonClick), event:.touchUpInside))
        pt.layer.cornerRadius = 12

        
        return pt
    }()
    lazy var nextButton:UIButton = {
        let pt = create_button(title: "", titleColor: .white,titleFont: SLFontConfig.medium18,bgColor: rgba(hex: "#1608EC"), image: nil, supView: self.backView, closure: { make in
            make.bottom.equalTo(-48)
            make.centerX.equalToSuperview()
            make.width.equalTo(281)
            make.height.equalTo(44)
        }, selector: (target: self, action: #selector(buttonClick), event:.touchUpInside))
        pt.layer.cornerRadius = 22
        
        return pt
    }()
    @objc func buttonClick(btn: UIButton){
        if btn == self.closeBtn{

        }else{
            self.dismiss(animated: true)
            var city = ""
            do {
                let row1 = try self.picker.selectedRow(inComponent: 0)
                city = city + self.datasA[row1]["suthat"].stringValue
                let row2 = try self.picker.selectedRow(inComponent: 1)
                city = city + "|" + self.datasB[row2]["suthat"].stringValue
                let row3 = try self.picker.selectedRow(inComponent: 2)
                city = city + "|" + self.datasC[row3]["suthat"].stringValue
            }catch{
                
            }

            if let block = self.handle{
                block(city)
            }
        }
    }
    lazy var picker:UIPickerView = {
        let pt = UIPickerView()
        pt.delegate = self
        pt.dataSource = self
//        pt.showsLargeContentViewer = false
        self.backView.addSubview(pt)
        pt.setValue(UIColor.black, forKey: "textColor")
        pt.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalTo(self.nextButton.snp.top)
            make.top.equalTo(50)
        }
        return pt
    }()

}
extension GFCityPicker: UIPickerViewDelegate,UIPickerViewDataSource{
    //设置选择框的总列数,继承于UIPickViewDataSource协议
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            if self.datasC.count > 0 {
                return 3
            }
            if self.datasB.count > 0 {
                return 2
            }
            return 1
        }
        //设置选择框的总行数,继承于UIPickViewDataSource协议
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            //总行数设置为数据源的总长度。component :为0 表示第一列，1 表示第二列
            //根据不同的数据源设置不同的个数

            if component == 0{
                return self.datasA.count
            }else if component == 1{
                return self.datasB.count
            }else{
                return self.datasC.count
            }
        }
        //设置选项框各选项的内容,继承于UIPickViewDelegate协议
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
            

            if component == 0{ //选择第一级数据
                return self.datasA[row]["suthat"].stringValue
            }
            else if component == 1{//选择第二级数据
                return self.datasB[row]["suthat"].stringValue
            }else{
                return self.datasC[row]["suthat"].stringValue
            }
        }
        //选择控件的事件选择
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            if(component == 0){
                // 根据第一列选择的值，获取第二列数据
                self.datasB = self.datasA[row]["quite"].arrayValue
                //刷新第二列的数据源
                self.picker.reloadComponent(1)
                self.picker.selectRow(0, inComponent: 1, animated: true)
                if self.datasB.count > 0{
                    let arr = self.datasB[0]["quite"].arrayValue
                    if arr.count > 0{
                        self.datasC = arr
                        self.picker.reloadComponent(2)
                        self.picker.selectRow(0, inComponent: 2, animated: true)
                    }
                }
                
            }else if component == 1{
                let arr = self.datasB[row]["quite"].arrayValue
                if arr.count > 0{
                    self.datasC = arr
                    self.picker.selectRow(0, inComponent: 2, animated: true)
                    self.picker.reloadComponent(2)
                }else{
//                    self.datasC = []
                }

            }
        }
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var titleStr = ""
        if component == 0{ //选择第一级数据
            titleStr = self.datasA[row]["suthat"].stringValue
        }
        else if component == 1{//选择第二级数据
            titleStr = self.datasB[row]["suthat"].stringValue
        }else{
            titleStr = self.datasC[row]["suthat"].stringValue
        }
        let pickerLabel = UILabel()
        pickerLabel.text = titleStr
        pickerLabel.font = SLFontConfig.medium13 // 设置字体大小
        pickerLabel.numberOfLines = 0
        pickerLabel.textColor = .black
        return pickerLabel
        }
        //设置每行选项的高度
        func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
            return 44.0
        }

}

